{--


Q-Bot 0.0.1 alpha

This is the first attempt to build a bot based on Q-learning 
For Lambda the gathering game.

@Version 0.0.1
@Author Benjamin Schubert

@DOTO
--Ruels
--Stick all togather
--debuging

--}

module Agent where

import Representation   as R
import Interaction      as I
import Q-Learning       as L
import Rules            as Ru

import System.IO
import System.Environment   
 
  
--constants--
q-file = "./q-matrix.dat" 
doLearn = True

main::IO()
main=do
        --init phase--
        game <-R.initGame
        if doesFileExists q-file
                then q<-readMatrixFromTextFile q-file
                else q<-R.initQ
        temp<-10000
        player <- head getArgs
        firstAction <- R.chooseAction temp q game
        
        if doLearn 
                then learnGame player firstActio game q temp      -- for learning the game by using Q-learning
                else playGame player firstAction game q temp       -- for playing the game with the current knowlage
                
                

{--
this function plays the game and learns the best strategy during several games
--}
learnGame::Int->Action->Game-Q->Int->IO()
learnGame player action game q temp 
                        -- if the game is over. Save Q in a file
                        | Ru.gameOver game = do writeMatrixToTextFile q-file q
                        --otherwise play a fucking card!    
                        | otherwise = 
                                -- do we first have to read?
                                if player == "1"
                                        then do
                                                opAction<-I.read
                                                oldLife <-(Ru.prob.life game) - (Ru.prob.life.game)             -- read the opponen's card 
                                                newGame<-Ru.updateGame True opAction action game                     -- update Game
                                                I.write action                                                  -- play proponent's card
                                                (newTemp,newAction,newQ)<-R.learn temp action game q oldLife    -- learn and get new card
                                                learnGame player newAction newGame newQ newTemp                 -- start new round
                                        else do
                                                I.write action                                                  -- play proponent's card
                                                opAction<-I.read                                                -- read the opponen's card
                                                oldLife <-(Ru.prob.life game) - (Ru.prob.life.game)             -- read the opponen's card 
                                                newGame<-Ru.updateGame False opAction action game                     -- update Game
                                                (newTemp,newAction,newQ)<-R.learn temp action game q oldLife    -- learn and get new card
                                                learnGame player newAction newGame newQ newTemp                 -- start new round
                                                
                                                
{--
this fucntion plays the game with learned data
-} 
playGame::Int->Action->Game->Q->Int->IO()
palyGame player action game q temp = undefined                               
                
        
        